package jike.demo15.code01;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

/**
 * @author oyz
 */
public class Serialization {

    private static final String IDENTIFIER_STRING = "UEUEUE;";

    private Gson gson;

    public Serialization() {
        this.gson = new Gson();
    }

    public String Serialize(Map<String, String> object){
        StringBuilder sb = new StringBuilder();
        sb.append(IDENTIFIER_STRING);
        sb.append(gson.toJson(object));
        return sb.toString();
    }

    public Map<String, String> deserialize(String text) {
         if (!text.startsWith(IDENTIFIER_STRING)) {
             return Collections.emptyMap();
             }
         String gsonStr = text.substring(IDENTIFIER_STRING.length());
         return gson.fromJson(gsonStr, Map.class);
         }

    class Gson{

        <T> String  toJson(T t){
            return t.toString();
        }

        public Map<String, String> fromJson(String gsonStr, Class<Map> mapClass) {
            Map<String, String> map = new HashMap<String, String>();
            map.put(gsonStr, mapClass.getName());
            return map;
        }
    }
 }





